Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
als-object-serializer
Advanced tools
An advanced JavaScript library for serializing and deserializing complex objects including those with circular references, functions, and special object types like Date, Set, and Map. Ideal for applications needing robust handling of object serialization
als-object-serializer
is an advanced JavaScript library designed for serializing and deserializing objects, including those with complex structures such as circular references, functions, Date
, RegExp
, Set
, and Map
objects. It uniquely preserves function definitions and manages circular dependencies within objects, making it ideal for scenarios where traditional JSON serialization is insufficient.
Where it can be used?
With als-object-serializer
you can save js objects in database, localStorage (it's can be used in browser too), as script binded from backend to frontend and more.
Install als-object-serializer
using npm:
npm install als-object-serializer
NodeJS usage
const Obj = require('als-object-serializer');
Browser usage
<script src="/node_modules/als-object-serializer/index.js"></script>
Here’s how to use als-object-serializer
to serialize and deserialize objects:
const myObject = {
name: "Alice",
details: { age: 25, hobbies: ["reading", "gaming"] }
};
const serialized = Obj.stringify(myObject);
//serialized = {"name":"Alice","details":{"age":25,"hobbies":["reading","gaming"]}}
const parsed = Obj.parse(serializedData);
// parsed should return an object identical to myObject
const organizers = [
{name:'Alice',phone:'565-5654545'}
]
const complexObject = {
date: new Date(),
events: new Set(['conference', 'meeting', 'webinar']),
organizers,
details: {
organizer: organizers[0],
contact: function () {
return `Contact Organizer: ${this.organizer.name}: ${this.organizer.phone}`;
},
}
};
const serialized = Obj.stringify(complexObject);
const parsed = Obj.parse(serialized)
console.log(serialized);
// {"date":new Date("2024-07-03T06:52:48.901Z"),"events":new Set(["conference","meeting","webinar"]),"organizers":[{"name":"Alice","phone":"565-5654545"}],"details":{"organizer":function recursiveReference(self) {return self['organizers'][0]},"contact":function () {\n return `Contact Organizer: ${this.organizer.name}: ${this.organizer.phone}`;\n }}}
console.log(parsed); // should return similar to complexObject object
console.log(parsed.organizers[0] === parsed.details.organizer) // true
console.log(parsed.details.contact()) // Contact Organizer: Alice: 565-5654545
In example above, cyclyc reference replaced with recursiveReference
function which had run in Obj.parse method to get the reference. The name of recursiveReference
available in Obj.recursiveName
and can be changed.
als-object-serializer
uniquely handles circular references by converting them into a string representation that includes a custom function, recursiveReference
. This function is designed to reconstruct the circular reference when parsed back into an object using the objParse
method. This approach ensures that complex object graphs with circular references can be safely converted to a string and back without losing their structure.
Note: The string output should be used with understanding that it includes JavaScript code that reconstructs circular relationships. When deserializing, ensure that the input string is from a trusted source as the parsing mechanism executes the code within the string.
Serialization of functions with objStringify
converts functions to their string representation using toString()
. This process captures the function's code but not its execution context, closures, or external references. Thus, while the function's syntax is preserved, it might not be restored to its original functional state upon deserialization. Use this feature with the understanding that some functions may require additional context to operate as intended.
new Function()
Due to the use of new Function()
in the objParse
method for executing string representation, it is crucial to ensure that the serialized string passed to objParse
is from a trusted and verified source. The execution of untrusted code can lead to serious security vulnerabilities similar to those associated with the use of eval()
. Always validate or sanitize inputs rigorously when dealing with serialization and deserialization to prevent security issues.
When using objParse
, ensure that the input string is from a trusted source. The function utilizes new Function()
to evaluate the string representation of an object, which can execute arbitrary code. As such, passing unverified or untrusted data to objParse
can lead to significant security risks similar to those associated with the eval()
function. Always validate or sanitize inputs to prevent security vulnerabilities.
FAQs
An advanced JavaScript library for serializing and deserializing complex objects including those with circular references, functions, and special object types like Date, Set, and Map. Ideal for applications needing robust handling of object serialization
The npm package als-object-serializer receives a total of 3 weekly downloads. As such, als-object-serializer popularity was classified as not popular.
We found that als-object-serializer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.